from PySide6.QtGui import Qt
from PySide6.QtWidgets import QWidget, QSizePolicy, QHBoxLayout, QVBoxLayout, QTextBrowser, QLabel

from common.player.reference import Player
from common.world import LatticeMap
from gui.reference import SIZE_WIDTH, SIZE_HEIGHT
from gui.signals import SIGNALS
from gui.widgets.double_col_view import EquipmentView
from gui.widgets.grid_widget import GridWidget
from gui.widgets.player_panel import PlayerPanelWidget
from gui.widgets.quick_bar import QuickBarWidget
from utils.db import DBSession
from utils.settings import VERSION


class MainBoard(QWidget):
    def __init__(self, player: Player):
        super().__init__()

        self.setWindowTitle(f'超级战士打造模拟器 v{VERSION}')
        self.setGeometry(300, 300, SIZE_WIDTH, SIZE_HEIGHT)
        self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
        self.setFixedSize(SIZE_WIDTH, SIZE_HEIGHT)

        self.player = player

        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        left_layout = QVBoxLayout()
        left_layout.setContentsMargins(0, 0, 0, 0)
        left_layout.setSpacing(0)

        right_layout = QVBoxLayout()
        right_layout.setContentsMargins(0, 0, 0, 0)
        right_layout.setSpacing(0)

        layout.addLayout(left_layout)
        layout.addLayout(right_layout)

        # 玩家移动空间
        self.grid = GridWidget(self, player)
        # 消息框
        self.msg = QTextBrowser(self)
        self.msg.setFocusPolicy(Qt.FocusPolicy.NoFocus)
        self.msg.setLineWrapMode(QTextBrowser.LineWrapMode.NoWrap)
        SIGNALS.send_message.connect(self.msg.append)
        # 位置标签
        self.locate_label = QLabel()
        self.change_locate_label()
        SIGNALS.change_locate.connect(self.change_locate_label)
        # 快捷入口按钮组
        self.quick_bar = QuickBarWidget(self, player)
        SIGNALS.change_quick_bar.connect(self.quick_bar.update_data)
        # 装备表
        self.equip_table = EquipmentView(player)
        SIGNALS.update_equip_table.connect(self.equip_table.update_data)
        # 基础面板
        panel_label = QLabel(
            '<p><span style="font-family:SimHei;font-weight: bold;font-size:25px">面板数据</span>'
            '<a href="/panel_detail" style="text-decoration: none">点击详情</a></p>'
        )
        panel_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.panel = PlayerPanelWidget(self, player)
        SIGNALS.update_player_penal.connect(self.panel.update_data)

        left_layout.addWidget(self.grid)
        left_layout.addWidget(self.msg)

        right_layout.addWidget(self.locate_label)
        right_layout.addWidget(self.quick_bar)
        right_layout.addWidget(self.equip_table)
        right_layout.addWidget(panel_label)
        right_layout.addWidget(self.panel)

        self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
        self.setFocus()

        SIGNALS.update_main_ui.connect(self.update_ui)

        # self.timer = QTimer(self)
        # self.timer.timeout.connect(self.stay_focus)
        # self.timer.start(1000)

    # def stay_focus(self):
    #     self.setFocus()

    def update_ui(self):
        self.quick_bar.update_data()
        self.equip_table.update_data()
        self.panel.update_data()

        self.update()

    def change_locate_label(self):
        lm: LatticeMap = DBSession.lattice_map[self.player.map_name]
        text1 = f'当前地图: <span style="color:blue;font-family:SimHei;font-size:14pt;">{lm.name}</span>'
        text2 = f' 当前位置: <span style="color:blue;font-family:SimHei;font-size:14pt;">{self.player.pos}</span>'

        result = text1 + text2
        if build := lm.get_build(self.player.pos):
            result += f' [<span style="color:red;font-family:SimHei;font-size:10pt;">{build.name}</span>]'
        elif npc := lm.get_npc(self.player.pos):
            result += f' [<span style="color:red;font-family:SimHei;font-size:10pt;">{npc.name}</span>]'

        self.locate_label.setText(result)
